<?php

namespace app\admin\controller\qingdongwms\notice;

use addons\qingdongwms\model\Product;
use app\admin\controller\qingdongwms\Base;
use addons\qingdongwms\model\Aftermarket as AftermarketModel;
use addons\qingdongwms\model\AftermarketType;
use think\DB;
use fast\Tree;

//信息库 售后问题
class Aftermarket extends Base
{

    /**
     * @var \addons\qingdongwms\model\Aftermarket
     */
    protected $model = null;
    protected $categorylist;
    protected $treeList;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new AftermarketModel();
        $this->typeModel = new AftermarketType();
        $this->categorylist = collection($this->typeModel->where([])->order('id desc')->select())->toArray();
        foreach ($this->categorylist as &$item) {
            if ($item['pid'] == 0) {
                $item['parent'] = '#';
            } else {
                $item['parent'] = $item['pid'];
            }
            $item['text'] = $item['name'];
            $item['state'] = ["opened" => true];
            $this->treeList[] = [
                $item['id'] => $item['name']
            ];
        }
        $tree = Tree::instance();
        $tree->init($this->categorylist, 'pid');
        $tree_list = $tree->getTreeList($tree->getTreeArray(0), 'name');
        $this->treeList = [];
        foreach ($tree_list as $k => $v) {
            $this->treeList[$v['id']] = $v['name'];
        }
       // dump($this->categorylist);exit;
        $this->assign('treeList', $this->treeList);
        $this->assignconfig('typeList', $this->categorylist);
    }


    public function index()
    {
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->where($where)
                ->order($sort, $order)->paginate($limit);
            $row = $list->items();
            $result = array("total" => $list->total(), "rows" => $row);

            return json($result);
        }

        return $this->view->fetch();
    }


    //添加
    public function add()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post('row/a');
            $typeIds = $this->request->post('type_ids/a');
            $productIds = $this->request->post('product_ids/a');
            $data['type_ids'] = ',' . implode(',', $typeIds) . ',';
            $data['product_ids'] = implode(',', $productIds);
            $result = $this->model->allowField(true)->save($data);
            if (!$result) {
                $this->error('提交失败');
            }
            $this->success('提交成功');
        }
        $this->assign('productList', Product::where([])->column('name','id'));
        return $this->view->fetch();
    }


    //修改
    public function edit($ids = null)
    {
        $map['id'] = $ids;
        if ($this->request->isAjax()) {
            $data = $this->request->post('row/a');

            $typeIds = $this->request->post('type_ids/a');
            $productIds = $this->request->post('product_ids/a');
            $data['type_ids'] = ',' . implode(',', $typeIds) . ',';
            $data['product_ids'] = implode(',', $productIds);
            $result = $this->model->isUpdate(true)->save($data, $map);
            if (!$result) {
                $this->error('修改失败');
            }
            $this->success('修改成功');
        }
        $data = $this->model::where($map)->find();
        $data['img'] = $data['img'] ? $data['img'] : '';
        $this->view->assign("row", $data);
        $this->assign('hasTypes', explode(',', trim($data['type_ids'], ',')));
        $this->assign('hasProducts', $data['product_ids']);
        $this->assign('productList', Product::where([])->column('name','id'));
        return $this->view->fetch();
    }


    //删除
    public function del($ids = null)
    {
        if ($this->request->isAjax()) {
            $map['id'] = array('in', $ids);
            $result = $this->model->destroy($map);
            if (!$result) {
                $this->error('删除失败');
            }
            $this->success('删除成功');
        }

        return $this->view->fetch();
    }
}